package com.weige.conver.handler;

import com.alibaba.fastjson.JSONObject;
import com.weige.conver.config.ActivitiConfig;
import com.weige.conver.entity.*;
import com.weige.conver.service.DeviceService;
import com.weige.conver.service.RabbitMQService;
import com.weige.conver.utils.DateUtils;
import com.weige.conver.utils.FileUtils;
import com.weige.conver.utils.JsonUtils;
import com.weige.conver.utils.StringSubUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.weige.conver.utils.DateUtils.getNowTime;

/**
 * 氧气设备物模型转换
 */
public class FenceHandler implements ConverHandler {

    RabbitMQService rabbitMQService = ActivitiConfig.getBean(RabbitMQService.class);
    DeviceService deviceService = ActivitiConfig.getBean(DeviceService.class);

    @SneakyThrows
    @Override
    public void handler(ReceiveMessage receiveMessage) throws IOException {
        Map siteMap = FileUtils.getBaseMap();
        String deviceId = receiveMessage.getDevId();
        //物联网平台数据配置
        String model = "fence";
        String manufactureId = "ShuyanyuanAIGateway";
        String serviceName = "securityEventService";
        //********
        SecurityEventService eventService = new SecurityEventService();
        eventService.setEventTime(getNowTime());
        eventService.setSiteId((String) siteMap.get("siteId"));
        eventService.setCameraId("");
        eventService.setPointId("");
        eventService.setImageBase64("");
        PloygonVertex ploygonVertex = new PloygonVertex();
        ploygonVertex.setX(0);
        ploygonVertex.setY(0);
        eventService.setPloygonVertex(ploygonVertex);
        eventService.setAlarmFlag(Integer.valueOf(receiveMessage.getValue()));
        List<Evelt> eveltList = new ArrayList<>();
        Evelt evelt = new Evelt();
        evelt.setPedestrianConfIdence(new BigDecimal("0"));
        PedestrianRectangle pedestrianRectangle = new PedestrianRectangle();
        pedestrianRectangle.setLtX(0);
        pedestrianRectangle.setLtY(0);
        pedestrianRectangle.setRbX(0);
        pedestrianRectangle.setRbY(0);
        evelt.setPedestrianRectangle(pedestrianRectangle);
        evelt.setHelmetAlarm(1);
        evelt.setHelmetConfIdence(new BigDecimal("0"));
        evelt.setSuitAlarm(0);
        evelt.setSuitConfIdence(new BigDecimal("0"));
        evelt.setBoundrayAlarm(1);
        evelt.setBoundrayConfIdence(new BigDecimal("0"));
        evelt.setPersonId("");
        evelt.setFaceConfIdence(new BigDecimal("0"));
        eveltList.add(evelt);
        eventService.setEveltList(eveltList);
        eventService.setFileFolder("");
        eventService.setFileName("");
        eventService.setTopicName("AIRecognition_securityEventService");

        //判断数据对象是否为空，并上传到RabbitMQ
        if (eventService != null){
            String data = JSONObject.toJSONString(eventService);
            rabbitMQService.sendData(data,deviceId,deviceId,model,manufactureId,serviceName);
        }
    }

}
